﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Net;
using BlackXS.Info;


namespace TM.Unofficial.Network
{
	/// <summary>
	/// Virtual Browser for Post and Get requests
	/// </summary>
    public class VBrowser
    {
        /// <summary>
        /// Browser Timout
        /// </summary>
        public Int32 TimeOut { get; set; }

        //Webrequest
        HttpWebRequest req;
        HttpWebResponse res;

        //CookieJar for Sessionhandling
        CookieContainer cookieJar;

        /// <summary>
        /// Creates a new VBrowser
        /// </summary>
        /// <param name="TimeOut">Connection Timeout</param>
        public VBrowser(int TimeOut)
		{
            //wird gebraucht damit man sich einloggen kann ((417) Expectation Failed) -> Does not really work global
			System.Net.ServicePointManager.Expect100Continue = false;
			cookieJar = new CookieContainer();
            this.TimeOut = TimeOut;
		}

        /// <summary>
        /// Simple Get WebRequest
        /// </summary>
        /// <param name="uri">File Path</param>
        /// <param name="SpoofRefferer">Spooth the refferer (just for Login)</param>
        /// <returns>Returns the content of the requestet URI</returns>
        public string GetRequest(Uri uri, bool SpoofRefferer)
        {
            if (SpoofRefferer)
            {
               req = (HttpWebRequest)WebRequest.Create(uri);
            }
            else
            {
                req = (HttpWebRequest)HttpWebRequest.Create(uri);
            }
            
            req.CookieContainer = cookieJar;
            req.Method = "GET";
            req.KeepAlive = true;
            req.ServicePoint.Expect100Continue = false;
            req.Referer = uri.ToString();
            req.Timeout = TimeOut;
            req.UserAgent = ("User-Agent: " + AssemblyInfo.AssemblyTitle + "/" + AssemblyInfo.AssemblyVersion);
            req.ContentType = "application/x-www-form-urlencoded";
            res = (HttpWebResponse)req.GetResponse();

            return (new StreamReader(res.GetResponseStream()).ReadToEnd());
        }

        /// <summary>
        /// Simple Post Request
        /// </summary>
        /// <param name="uri">File Path</param>
        /// <param name="PostDataBytes">Data for the Service</param>
        /// <param name="SpoofRefferer">Spooth the refferer (just for Login)</param>
        /// <returns>Returns the content of the requestet URI</returns>
        public string PostRequest(Uri uri, byte[] PostDataBytes, bool SpoofRefferer)
        {
            if (SpoofRefferer)
            {
                req = (HttpWebRequest)WebRequest.Create(uri);
            }
            else
            {
                req = (HttpWebRequest)HttpWebRequest.Create(uri);
            }

            req.CookieContainer = cookieJar;
            req.Method = "POST";
            req.KeepAlive = false;
            req.Referer = uri.ToString();
            req.ServicePoint.Expect100Continue = false;
            req.Timeout = (TimeOut);
            req.UserAgent = ("User-Agent: " + AssemblyInfo.AssemblyTitle + "/" + AssemblyInfo.AssemblyVersion);
            req.ContentType = "application/x-www-form-urlencoded";

            req.ContentLength = PostDataBytes.Length;
            Stream stream = req.GetRequestStream();
            stream.Write(PostDataBytes, 0, PostDataBytes.Length);
            stream.Close();

            res = (HttpWebResponse)req.GetResponse();

            return (new StreamReader(res.GetResponseStream()).ReadToEnd());
        }
    }
}
